/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.scmt.hmzd.controller;

import com.scmt.core.common.exception.ScmtException;
import com.scmt.core.common.utils.ResultUtil;
import com.scmt.core.common.vo.Result;
import com.scmt.hmzd.config.WxConfig;
import com.scmt.hmzd.utils.HttpClient;
import com.scmt.hmzd.yx.YxUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * jsapi 签名
 * @author yami
 */
@RestController
@RequestMapping("/scmt/appMain")
@Api(tags = "微信jsapi接口")
@AllArgsConstructor
@Slf4j
public class JsapiController {

    @Autowired
    private WxConfig wxConfig;

    @RequestMapping("/createJsapiSignature")
    @ApiOperation(value = "生成jsapi签名", notes = "根据url生成当前页面的jsapi签名")
    public Result<Object> createJsapiSignature(@RequestParam String url) {
        WxJsapiSignature jsapiSignature;
        try {
            jsapiSignature = wxConfig.getWxMpService().createJsapiSignature(url);
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("appId", jsapiSignature.getAppId());
            hashMap.put("nonceStr", jsapiSignature.getNonceStr());
            hashMap.put("timestamp", String.valueOf(jsapiSignature.getTimestamp()));
            hashMap.put("url", jsapiSignature.getUrl());
            hashMap.put("signature", jsapiSignature.getSignature());
            return ResultUtil.data(hashMap);
        } catch (WxErrorException e) {
            throw new ScmtException(e.getError().toString());
        }
    }


    @RequestMapping("/testCallPhone")
    public Result<Object> testCallPhone() {
        YxUtils.testAx();
        return null;
    }



    @RequestMapping("/queryMapData")
    public Result<Object> queryMapData(@RequestParam String from, @RequestParam String to) {
        log.info("form:{}", from);
        log.info("to:{}", to);
        LinkedMultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<>();
        requestMap.add("from", from);
        requestMap.add("to", to);
        requestMap.add("key", "OZZBZ-YZYRI-A5PGJ-UFIG4-VVG46-A3FJ2");
        String request = HttpClient.sendGetRequest("https://apis.map.qq.com/ws/direction/v1/driving?from=" + from + "&to=" + to + "&key=OZZBZ-YZYRI-A5PGJ-UFIG4-VVG46-A3FJ2" , requestMap, null);
        log.info("request:{}", request);
        return ResultUtil.data(request);
    }
}
